package com.eshealthy.serviceImpl;

import com.eshealthy.dao.DoctorDao;
import com.eshealthy.entity.*;
import com.eshealthy.service.DoctorService;
import com.eshealthy.utils.EsCase;
import com.eshealthy.utils.EsLibrary;
import com.eshealthy.utils.FullSearch;
import com.eshealthy.utils.Week;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorDao doctorDao;
//医生注册
    @Override
    public void register(Doctor doctor) {
        Doctor doctorDB=doctorDao.findAllDoctor(doctor.getId_card());
        if(doctorDB==null){
            doctorDao.save(doctor);
        }else{
            throw new RuntimeException("用户已存在");
        }


    }
//医生登录
    @Override
    public Doctor login(Doctor doctor) {
        Doctor doctorDB=doctorDao.findAllDoctor(doctor.getTel());
        if(!ObjectUtils.isEmpty(doctorDB)){
           if(doctorDB.getPassword().equals(doctor.getPassword())){
               return doctorDB;
           }else{
               throw new RuntimeException("用户密码输入不正确");
           }
        }else{
            throw new RuntimeException("用户输入错误");
        }

    }

    @Override
    public void registerArea(Integer area) {
       doctorDao.registerArea(area);
    }

    @Override
    public List<Cases> findCases(Integer page, Integer rows) throws IOException {
        EsCase esCase=new EsCase();
        return esCase.CaseList(page,rows);
    }

    @Override
    public List<Scheduling> findTime(String aid,String str) {
        return doctorDao.findTime(aid,str);
    }

    @Override
    public Integer findTotalsSchedule(Integer id) {
        return doctorDao.findTotalsSchedule(id);
    }

    @Override
    public List<Patient> findSchedule(Integer id,Integer page,Integer rows) {
        int start=(page-1)*rows;
        return doctorDao.findSchedule(id,start,rows);
    }

    @Override
    public int[] week() {
        Week week=new Week();
        return week.oneWeek();
    }

    @Override
    public void updateStatus(Integer id) {
        doctorDao.updateStatus(id);
    }

    @Override
    public Patient findOne(Integer id) {
        return doctorDao.findOne(id);
    }

    @Override
    public String pname(Integer id) {
        return doctorDao.pname(id);
    }

    @Override
    public Doctor check(Integer id) {
       return doctorDao.check(id);
    }



    @Override
    public void addCase(Cases cases)throws IOException {
        EsCase esCase=new EsCase();
        esCase.add(cases);
    }

    @Override
    public List<Drug> searchDrug(Integer page,Integer rows,String word) throws IOException {
        FullSearch fullSearch=new FullSearch();
        return fullSearch.Drugs(page,rows,word);
    }

    @Override
    public long Totals(Integer id,String key) throws IOException {
        FullSearch fullSearch=new FullSearch();
        return fullSearch.count(id,key);
    }

    @Override
    public List<MedicalDevice> searchDevice(Integer page, Integer rows, String word) throws IOException {
        FullSearch fullSearch=new FullSearch();
        return fullSearch.Devices(page,rows,word);
    }

    @Override
    public List<Library> searchLibrary(Integer page, Integer rows, String word) throws IOException {
        FullSearch fullSearch=new FullSearch();
        return fullSearch.Librarys(page,rows,word);
    }



    @Override
    public long findTotalsCase() throws IOException {
        EsCase esCase = new EsCase();
        return esCase.count();
    }

    @Override
    public String findPid(Integer id) {
        return doctorDao.findPid(id);
    }


}
